﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Xml;
using MTGProject;

namespace CardFetcher
{
    public partial class Form1 : Form
    {

        string[] cardsAvailableXML;
        string[] cardsAvailableImage;

        public Form1()
        {
            InitializeComponent();

            //build list of paths for card XML files
            cardsAvailableXML = Directory.GetFiles(CardIO.cardPath);

            //build list of paths for card images
            cardsAvailableImage = new string[cardsAvailableXML.Length];
            for (int i = 0; i < cardsAvailableXML.Length; i++)
            {
                string imagePath = cardsAvailableXML[i];

                imagePath = imagePath.Substring(CardIO.cardPath.Length);
                imagePath = imagePath.Substring(0, imagePath.Length - 4);
                imagePath = "images\\" + imagePath + ".jpg";

                cardsAvailableImage[i] = imagePath;
            }

            updateGUI();
        }

        private void updateGUI()
        {
            int cardsTotal = cardsAvailableXML.Length;
            int cardsWithImage = 0;

            //update total number of cards
            LabelCountCardsAvailable.Text = cardsTotal.ToString();

            //update number of cards with image
            for (int i = 0; i < cardsAvailableImage.Length; i++)
            {
                if (File.Exists(cardsAvailableImage[i]))
                    cardsWithImage++;
            }
            labelCountCardsWithImages.Text = cardsWithImage.ToString();

            //update number of cards missing their image
            labelCountCardsWithoutImages.Text = (cardsTotal - cardsWithImage).ToString();

            //update progress bar
            progressBarDownload.Value = (int)((cardsWithImage / (double)cardsTotal) * 100);
        }

        private void buttonDownloadMissingCards_Click(object sender, EventArgs e)
        {
            WebClient downloadClient = new WebClient();
            downloadClient.Proxy = null;

            for (int i = 0; i < cardsAvailableXML.Length; i++)
            {

                if (!File.Exists(cardsAvailableImage[i]))
                {
                    string cardDownloadURL = "";

                    XmlDocument doc = new XmlDocument();
                    doc.Load(cardsAvailableXML[i]);
                    XmlNodeList fileMoveList = doc.GetElementsByTagName("card");

                    //HACK: each xml should only contain one entry so this will run once.
                    foreach (XmlNode node in fileMoveList)
                        cardDownloadURL = ((XmlElement)node).GetElementsByTagName("imageurl")[0].InnerText;

                    if (cardDownloadURL != null && cardDownloadURL.Length > 0)
                    {
                        try
                        {
                            downloadClient.DownloadFile(cardDownloadURL, cardsAvailableImage[i]);
                            updateGUI();
                        }
                        catch (Exception clientException)
                        {
                            MessageBox.Show(clientException.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Card does not include image URL.");
                    }
                }
            }

            //download card back
            if(!File.Exists("images\\generic_cardback.jpg"))
            {
                try
                {
                    downloadClient.DownloadFile("http://images.boardgamegeek.com/images/pic163749_md.jpg", "images\\generic_cardback.jpg");
                    updateGUI();
                }
                catch (Exception clientException)
                {
                    MessageBox.Show(clientException.Message);
                }
            }
            
            MessageBox.Show("All card images possible have been downloaded");
        }
    }
}
